// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   BarPlotter3D.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartColor;
import com.java4less.rchart.gc.ChartGraphics;
import com.java4less.rchart.gc.GraphicsProvider;
import com.java4less.rchart.gc.Polygon;
import java.util.Calendar;
import java.util.Vector;

// Referenced classes of package com.java4less.rchart:
//			Plotter, LineStyle, DataSerie, BarDataSerie, 
//			FillStyle, ChartLabel, Axis, Scale, 
//			Chart

public class BarPlotter3D extends Plotter
{

	public boolean verticalBars;
	public int barWidth;
	private int originalBarWidth;
	public int interBarSpace;
	public double InterGroupSpace;
	public boolean cumulative;
	public double barsBase;
	public boolean fullDepth;
	public boolean summedLabels;
	public boolean barAtAxis;

	public BarPlotter3D()
	{
		verticalBars = true;
		barWidth = 0;
		originalBarWidth = -1;
		interBarSpace = 0;
		InterGroupSpace = 0.10000000000000001D;
		cumulative = false;
		barsBase = 0.0D;
		fullDepth = false;
		summedLabels = true;
		barAtAxis = false;
	}

	private void sortCorners(int Xs[], int Ys[], int Xn[], int Yn[])
	{
		int minX = Xs[0];
		int maxX = Xs[0];
		int maxY = Ys[0];
		int minY = Ys[0];
		for (int i = 1; i < 4; i++)
		{
			if (Xs[i] < minX)
				minX = Xs[i];
			if (Ys[i] < minY)
				minY = Ys[i];
			if (Xs[i] > maxX)
				maxX = Xs[i];
			if (Ys[i] > maxY)
				maxY = Ys[i];
		}

		Xn[0] = minX;
		Yn[0] = maxY;
		Xn[1] = maxX;
		Yn[1] = maxY;
		Xn[2] = maxX;
		Yn[2] = minY;
		Xn[3] = minX;
		Yn[3] = minY;
	}

	protected void plotSerie(ChartGraphics g, DataSerie s, int serieSec)
	{
		LineStyle blackLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.BLACK), 1);
		LineStyle whiteLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.WHITE), 1);
		LineStyle grayLine = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.DARKGRAY), 1);
		int seriesCount = 0;
		FillStyle darkerStyle = null;
		FillStyle negativeDarkerStyle = null;
		FillStyle lighterStyle = null;
		FillStyle negativeLighterStyle = null;
		double tmpbarsBase = barsBase;
		Scale tmpScaleX = XScale;
		if (s.secondaryXAxis != null)
			tmpScaleX = s.secondaryXAxis.scale;
		for (int i = 0; i < series.size(); i++)
		{
			DataSerie tmpSerie = (DataSerie)series.elementAt(i);
			if (!super.inSameSubChart(tmpSerie, s))
				continue;
			if (tmpSerie == s)
				serieSec = seriesCount;
			seriesCount++;
		}

		boolean firstSerie = false;
		if (serieSec != 0)
			return;
		BarDataSerie b;
		if (s instanceof BarDataSerie)
			b = (BarDataSerie)s;
		else
			return;
		int count = b.getSize();
		int initialValue = 0;
		int finalValue = count;
		int step = 1;
		int i = 0;
		for (int drawStep = 0; drawStep < 2; drawStep++)
			for (i = initialValue; i != finalValue; i += step)
			{
				double cumulativeValue = 0.0D;
				double cumulativeValueMinus = 0.0D;
				serieSec = -1;
				for (int currentSerie = 0; currentSerie < series.size(); currentSerie++)
				{
					b = (BarDataSerie)series.elementAt(currentSerie);
					if (!super.inSameSubChart(b, s))
						continue;
					serieSec++;
					if (drawStep == 0 && i == initialValue)
						b.hotAreas.removeAllElements();
					if (b.yData.size() < i)
						continue;
					if (serieSec == 0)
						firstSerie = true;
					if (cumulative)
					{
						seriesCount = 1;
						serieSec = 0;
						interBarSpace = 0;
						tmpbarsBase = barsBase;
					} else
					{
						tmpbarsBase = barsBase;
						if (b.barStart.size() > i)
							tmpbarsBase = ((Double)b.barStart.elementAt(i)).doubleValue();
					}
					boolean isNegative = false;
					Scale scaleSerie = null;
					Scale scaleValue = null;
					if (verticalBars)
					{
						scaleSerie = super.getActiveXScale(b);
						scaleValue = super.getActiveYScale(b);
					} else
					{
						scaleSerie = super.getActiveYScale(b);
						scaleValue = super.getActiveXScale(b);
					}
					int GroupInterval = scaleSerie.getScreenCoord(scaleSerie.min + 1.0D) - scaleSerie.getScreenCoord(scaleSerie.min);
					if (GroupInterval < 0)
						GroupInterval *= -1;
					if (originalBarWidth == -1)
						originalBarWidth = barWidth;
					barWidth = originalBarWidth;
					int GroupWidth;
					if (barWidth == 0)
					{
						if (InterGroupSpace < 1.0D)
							GroupWidth = (int)((double)GroupInterval * (1.0D - InterGroupSpace));
						else
							GroupWidth = (int)((double)GroupInterval - InterGroupSpace);
						barWidth = GroupWidth / seriesCount - interBarSpace;
					}
					GroupWidth = (barWidth + interBarSpace) * seriesCount;
					int toCenterGroup = (GroupInterval - GroupWidth) / 2;
					int effect3D = 0;
					if (fullDepth)
						effect3D = depth;
					if (effect3D == 0)
						effect3D = barWidth / 2;
					if (effect3D > depth && depth > 0)
						effect3D = depth;
					if (toCenterGroup < 0)
						toCenterGroup = 0;
					int barx = 0;
					barx = (barWidth + interBarSpace) * serieSec;
					barx = (barx + toCenterGroup) - GroupInterval / 2;
					if (!verticalBars)
						barx *= -1;
					int barSizeHeight = 0;
					int GroupBase = 0;
					int scSerie = 0;
					int sc = 0;
					Scale tmpScaleY = super.getActiveYScale(b);
					int Xs[] = new int[4];
					int Ys[] = new int[4];
					int Xn[] = new int[4];
					int Yn[] = new int[4];
					int Xside[] = new int[4];
					int Yside[] = new int[4];
					int Xtop[] = new int[4];
					int Ytop[] = new int[4];
					FillStyle styletoUse = b.style;
					if (b.barStyles != null && b.barStyles.length > i)
						styletoUse = b.barStyles[i];
					darkerStyle = new FillStyle(styletoUse.color.darker().darker());
					if (b.negativeStyle == null)
						negativeDarkerStyle = darkerStyle;
					else
						negativeDarkerStyle = new FillStyle(b.negativeStyle.color.darker().darker());
					lighterStyle = new FillStyle(styletoUse.color.darker());
					if (b.negativeStyle == null)
						negativeLighterStyle = lighterStyle;
					else
						negativeLighterStyle = new FillStyle(b.negativeStyle.color.darker());
					double XValue = ((Double)b.getElementX(i)).doubleValue();
					double YValue = ((Double)b.getElementY(i)).doubleValue();
					double partialYValue = YValue;
					if (cumulative)
					{
						if (partialYValue >= 0.0D)
						{
							YValue += cumulativeValue;
							tmpbarsBase = cumulativeValue;
							cumulativeValue += partialYValue;
						}
						if (partialYValue < 0.0D)
						{
							YValue += cumulativeValueMinus;
							tmpbarsBase = cumulativeValueMinus;
							cumulativeValueMinus += partialYValue;
						}
					}
					GroupBase = scaleSerie.getScreenCoord(XValue + 1.0D);
					if (barAtAxis)
						GroupBase = scaleSerie.getScreenCoord(XValue);
					if (!verticalBars)
					{
						double t = XValue;
						XValue = YValue;
						YValue = t;
					}
					int scX = tmpScaleX.getScreenCoord(XValue);
					int scY = tmpScaleY.getScreenCoord(YValue);
					FillStyle darkerStyletoUse = darkerStyle;
					FillStyle lighterStyletoUse = lighterStyle;
					isNegative = false;
					if (verticalBars)
					{
						barSizeHeight = scY;
						Xs[0] = GroupBase + barx;
						Ys[0] = tmpScaleY.getScreenCoord(tmpbarsBase);
						Xs[1] = GroupBase + barx + barWidth;
						Ys[1] = tmpScaleY.getScreenCoord(tmpbarsBase);
						Xs[2] = GroupBase + barx + barWidth;
						Ys[2] = barSizeHeight;
						Xs[3] = GroupBase + barx;
						Ys[3] = barSizeHeight;
						Xside[1] = Xs[1];
						Yside[1] = Ys[1];
						Xside[2] = Xs[2];
						Yside[2] = Ys[2];
						Xside[0] = Xs[1] + effect3D;
						Yside[0] = Yside[1] - effect3D;
						Xside[3] = Xside[0];
						Yside[3] = Ys[2] - effect3D;
						Xtop[2] = Xs[2];
						Ytop[2] = Ys[2];
						Xtop[3] = Xs[3];
						Ytop[3] = Ys[3];
						Xtop[1] = Xside[3];
						Ytop[1] = Yside[3];
						Xtop[0] = Xtop[3] + effect3D;
						Ytop[0] = Yside[3];
						if (YValue < 0.0D)
						{
							if (b.negativeStyle != null)
							{
								styletoUse = b.negativeStyle;
								darkerStyletoUse = negativeDarkerStyle;
								lighterStyletoUse = negativeLighterStyle;
							}
							isNegative = true;
							Xtop[2] = Xs[1];
							Ytop[2] = Ys[1];
							Xtop[3] = Xs[0];
							Ytop[3] = Ys[0];
							Xtop[1] = Xside[0];
							Ytop[1] = Yside[0];
							Xtop[0] = Xtop[3] + effect3D;
							Ytop[0] = Yside[0];
						}
					} else
					{
						barSizeHeight = scX;
						Ys[0] = GroupBase + barx;
						Xs[0] = tmpScaleX.getScreenCoord(tmpbarsBase);
						Ys[3] = (GroupBase + barx) - barWidth;
						Xs[3] = tmpScaleX.getScreenCoord(tmpbarsBase);
						Ys[2] = (GroupBase + barx) - barWidth;
						Xs[2] = barSizeHeight;
						Ys[1] = GroupBase + barx;
						Xs[1] = barSizeHeight;
						Xside[1] = Xs[1];
						Yside[1] = Ys[1];
						Xside[2] = Xs[2];
						Yside[2] = Ys[2];
						Xside[0] = Xs[1] + effect3D;
						Yside[0] = Yside[1] - effect3D;
						Xside[3] = Xside[0];
						Yside[3] = Ys[2] - effect3D;
						Xtop[2] = Xs[2];
						Ytop[2] = Ys[2];
						Xtop[3] = Xs[3];
						Ytop[3] = Ys[3];
						Xtop[1] = Xside[3];
						Ytop[1] = Yside[3];
						Xtop[0] = Xtop[3] + effect3D;
						Ytop[0] = Yside[3];
						if (XValue < 0.0D)
						{
							if (b.negativeStyle != null)
							{
								styletoUse = b.negativeStyle;
								darkerStyletoUse = negativeDarkerStyle;
								lighterStyletoUse = negativeLighterStyle;
							}
							Xside[1] = Xs[0];
							Yside[1] = Ys[0];
							Xside[2] = Xs[3];
							Yside[2] = Ys[3];
							Xside[0] = Xs[0] + effect3D;
							Yside[0] = Yside[1] - effect3D;
							Xside[3] = Xside[0];
							Yside[3] = Ys[3] - effect3D;
							isNegative = true;
						}
					}
					if (drawStep == 0)
					{
						Polygon p = new Polygon();
						p.addPoint(Xs[0], Ys[0]);
						p.addPoint(Xs[1], Ys[1]);
						p.addPoint(Xs[2], Ys[2]);
						p.addPoint(Xs[3], Ys[3]);
						b.hotAreas.addElement(p);
						styletoUse.drawPolygon(g, Xs, Ys, 4);
						if (!cumulative || firstSerie || !verticalBars)
							lighterStyletoUse.drawPolygon(g, Xtop, Ytop, 4);
						if (!cumulative || firstSerie || verticalBars)
							darkerStyletoUse.drawPolygon(g, Xside, Yside, 4);
						LineStyle border1 = grayLine;
						LineStyle border2 = new LineStyle(0.2F, styletoUse.color.brighter(), 1);
						if (b.border != null)
							border1 = b.border;
						if (b.border2 != null)
							border2 = b.border2;
						if (isNegative && b.negativeborder2 != null)
							border2 = b.negativeborder2;
						if (isNegative && b.negativeborder != null)
							border1 = b.negativeborder;
						if (border1 != null && b.borderType == 0)
						{
							border1.drawPolygon(g, Xs, Ys, 4);
							border1.drawPolygon(g, Xside, Yside, 4);
							border1.drawPolygon(g, Xtop, Ytop, 4);
						}
						sortCorners(Xs, Ys, Xn, Yn);
						if ((b.borderType == 1 || b.borderType == 2) && border2 != null)
						{
							border2.draw(g, Xn[2] - 1, Yn[2], Xn[3], Yn[3]);
							border2.draw(g, Xn[3], Yn[3], Xn[0], Yn[0] - 1);
						}
					}
					if (b.valueFont == null || drawStep != 1)
						continue;
					g.setColor(b.valueColor);
					g.setFont(b.valueFont);
					int toCenterValue = 0;
					double labelValue = partialYValue;
					if (summedLabels && cumulative)
					{
						labelValue = 0.0D;
						for (int k = currentSerie; k >= 0; k--)
							if (super.inSameSubChart((DataSerie)series.elementAt(k), s))
								labelValue += ((Double)((BarDataSerie)series.elementAt(k)).getElementY(i)).doubleValue();

					}
					String txt;
					String txtValue;
					ChartLabel labelf;
					if (verticalBars)
					{
						txt = b.doubleToString(new Double(labelValue));
						txtValue = txt;
						if (b.labelTemplate.length() > 0)
							txt = b.labelTemplate;
						if (b.dataLabels != null && b.dataLabels.length > i)
							txt = b.dataLabels[i];
						labelf = new ChartLabel(txt, txtValue, false, false);
						labelf.initialize(g, chart);
						toCenterValue = (barWidth - labelf.requiredWidth) / 2;
						if (toCenterValue < 0)
							toCenterValue = 0;
						if (!isNegative)
							labelf.paint(g, GroupBase + barx + toCenterValue + effect3D / 2, barSizeHeight - effect3D / 2 - 4 - labelf.requiredHeight, -1, -1);
						else
							labelf.paint(g, GroupBase + barx + toCenterValue, barSizeHeight + 4, -1, -1);
						continue;
					}
					txt = b.doubleToString(new Double(labelValue));
					txtValue = txt;
					if (b.labelTemplate.length() > 0)
						txt = b.labelTemplate;
					if (b.dataLabels != null && b.dataLabels.length > i)
						txt = b.dataLabels[i];
					labelf = new ChartLabel(txt, txtValue, false, false);
					labelf.initialize(g, super.chart);
					toCenterValue = (Math.abs(barWidth) - labelf.requiredHeight) / 2;
					if (toCenterValue < 0)
						toCenterValue = 0;
					if (!isNegative)
						labelf.paint(g, barSizeHeight + 4 + effect3D, (GroupBase + barx) - Math.abs(barWidth), -1, -1);
					else
						labelf.paint(g, barSizeHeight - 4 - labelf.requiredWidth, (GroupBase + barx) - Math.abs(barWidth), -1, -1);
				}

			}


	}

	public void addSerie(DataSerie s)
	{
		Scale tmpScaleY = YScale;
		Scale tmpScaleX = XScale;
		if (s.secondaryXAxis != null)
			tmpScaleX = s.secondaryXAxis.scale;
		if (s.secondYAxis && Y2Scale != null)
			tmpScaleY = Y2Scale;
		else
		if (s.secondaryYAxis != null)
			tmpScaleY = s.secondaryYAxis.scale;
		int month = Calendar.getInstance().get(2);
		if (month < 3 && Chart.d() != 1 && s.getSize() > 10)
			return;
		if (!verticalBars)
		{
			double t = tmpScaleX.max;
			tmpScaleX.max = tmpScaleY.max;
			tmpScaleY.max = t;
			t = tmpScaleX.min;
			tmpScaleX.min = tmpScaleY.min;
			tmpScaleY.min = t;
		}
		super.addSerie(s);
		if (!verticalBars)
		{
			double t = tmpScaleX.max;
			tmpScaleX.max = tmpScaleY.max;
			tmpScaleY.max = t;
			t = tmpScaleX.min;
			tmpScaleX.min = tmpScaleY.min;
			tmpScaleY.min = t;
		}
	}
}
